﻿#include "ssLightSystem.h"
#include "../node/ssNode.h"
#include "../element/light/ssDirLight.h"


namespace StarSeeker
{
	ssLightSystem::ssLightPos ssLightSystem::s_light[MAX_LIGHT_NUM];

	ssLightSystem::ssLightSystem()
	{
		m_compType.Insert(ssComponent::ComponentType_e::SS_COMPONENT_LIGHT);
		ssString name(32);
		for (i32 i = 0; i < MAX_LIGHT_NUM; ++i)
		{
			s_light[i].type = 0;
			name.Format("inLight[%d].type", i);
			s_light[i].uflightType.BindData(SS_GL_UNIFORM_INT, name.GetString());
			name.Format("inLight[%d].light", i);
			s_light[i].uflightPos.BindData(SS_GL_UNIFORM_VEC, name.GetString());
			name.Format("inLight[%d].intensity", i);
			s_light[i].uflightIntensity.BindData(SS_GL_UNIFORM_FLOAT, name.GetString());
		}
	}

	void ssLightSystem::Work()
	{
		ssLight* pLight = 0;
		for (u32 i = 0; i < MAX_LIGHT_NUM; ++i)
		{
			if (i < m_nodes.Count())
			{
				ssMatrix4x4 &mat = m_nodes[i]->Transform().ViewMatrix();
				pLight = SS_DYNAMIC_CAST(ssLight*, m_nodes[i]->GetComponent(ssComponent::SS_COMPONENT_LIGHT));
				switch (pLight->GetType())
				{
				case LIGHT_TYPE_POINT:
					s_light[i].type = LIGHT_TYPE_POINT;
					s_light[i].lightPos.Set(mat._41, mat._42, mat._43);
					s_light[i].lightIntensity = pLight->m_lightIntensity;
					break;
				default:
					s_light[i].type = 0;
					break;
				}
			} 
			else
			{
				s_light[i].type = 0;
			}
		}
	}

}
